package com.kun.valiant.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kun.valiant.activity.domain.Activity;
import com.kun.valiant.activity.domain.vo.ActivityVO;
import com.kun.valiant.activity.mapper.ActivityMapper;
import com.kun.valiant.activity.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.domain.model.PieData;
import com.kun.valiant.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 活动管理 服务实现类
 * </p>
 *
 * @author fangkun
 * @since 2023-06-05
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    ActivityMapper activityMapper;

    @Override
    public List<ActivityVO> selectActivityList(ActivityVO activityVO) {
        //去除过期活动
        expireActivity();
        return activityMapper.selectActivityList(activityVO);
    }

    @Override
    public AjaxResult addActivity(ActivityVO activityVO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityVO,activity);
        activity.setCreateBy(SecurityUtils.getUsername());
        activity.setCreateTime(LocalDateTime.now());
        activity.setStatus(1);
        activity.setDelFlag(0);
        return save(activity) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    @Override
    public AjaxResult editActivity(ActivityVO activityVO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityVO,activity);
        activity.setUpdateTime(LocalDateTime.now());
        activity.setUpdateBy(SecurityUtils.getUsername());

        //设置为null意思是不允许修改
        activity.setCreateBy(null);
        activity.setCreateTime(null);
        activity.setDelFlag(null);
        return updateById(activity) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Override
    public ActivityVO getActivityById(Integer activityId) {
        Activity activity = getById(activityId);
        ActivityVO activityVO = new ActivityVO();
        BeanUtils.copyProperties(activity,activityVO);
        return activityVO;
    }

    @Override
    public boolean removeActivityByIds(Integer[] activityIds) {

        LambdaUpdateWrapper<Activity> uq = new LambdaUpdateWrapper<>();
        uq.set(Activity::getDelFlag,1).in(Activity::getActivityId,activityIds);
        return update(uq);
    }

    @Override
    public List<Activity> getActivityByChannelId(Long channelId) {

        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Activity::getChannelId,channelId);
        return list(lqw);
    }

    //将超过当前时间的活动设置为过期
    private boolean expireActivity(){
        LambdaUpdateWrapper<Activity> uw = new LambdaUpdateWrapper<>();
        uw.set(Activity::getStatus,0).eq(Activity::getStatus,1).lt(Activity::getEndTime, LocalDateTime.now());
        return update(uw);
    }

    @Override
    public AjaxResult activityAnalysisData(ActivityVO activityVO) {
        if (activityVO.getParams().get("beginTime")==null||activityVO.getParams().get("endTime")==null) {
            activityVO.getParams().put("beginTime", LocalDateTime.now().minusWeeks(1));
            activityVO.getParams().put("endTime", LocalDateTime.now().plusYears(1));
        }
        List<PieData> list = activityMapper.activityAnalysisData(activityVO);
        return AjaxResult.success(list);
    }
}
